package cn.tedu.buycradserver.service.impl;

import cn.tedu.buycradserver.ex.ServiceException;
import cn.tedu.buycradserver.mapper.CategoryMapper;
import cn.tedu.buycradserver.pojo.dto.CategoryAddNewDTO;
import cn.tedu.buycradserver.pojo.entity.Category;
import cn.tedu.buycradserver.pojo.vo.CategoryDetailVO;
import cn.tedu.buycradserver.service.ICategoryServer;
import cn.tedu.buycradserver.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import cn.tedu.buycradserver.pojo.vo.CategoryListltemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CategoryServerImpl implements ICategoryServer {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void addNew(CategoryAddNewDTO categoryAddNewDTO) {
      log.debug("开始处理添加类别，参数{}",categoryAddNewDTO);
      Long parentId = categoryAddNewDTO.getParentId();
        CategoryDetailVO parentCategory = null;
        if (parentId !=null){
            parentCategory = categoryMapper.getById(parentId);
            if(parentCategory==null){
                String message = "添加类别失败，父级别不存在";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }
        String name = categoryAddNewDTO.getName();
        int count = categoryMapper.countByName(name);
        if (count>0){
            String message ="添加失败，类别名称【" + name + "】已经被占用！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryAddNewDTO,category);
        if (parentId==0){
            category.setDepth(1);
        }else {
            category.setDepth(parentCategory.getDepth()+1);
        }category.setIsParent(0);
        log.debug("即将像表格写入数据{}",category);
        int rows = categoryMapper.insert(category);
        if(rows!=1){
            String message="添加类别失败，服务器忙稍后尝试【错误码1】";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        if (parentId !=0&&parentCategory.getIsParent()==0){
            Category c = new Category();
            c.setId(parentId);
            c.setIsParent(1);
            rows = categoryMapper.updateById(c);
        }
        if (rows != 1){
            String message = "添加类别失败服务器忙，【错误码2】";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理删除业务，id={}",id);
        CategoryDetailVO queryResult = categoryMapper.getById(id);
        if(queryResult==null){
            String message ="删除类别失败(id="+id+")不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        {
            int count = categoryMapper.countByParentId(id);
            if(count>0){
                String message = "删除类别失败，当前类别存在子集，不能删除";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }
        }
        int rows = categoryMapper.deleteById(id);
        if(rows!=1){
            String message="删除失败,服务器忙，请稍后尝试错误码【1】";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        Long paremtId = queryResult.getParentId();
        int count = categoryMapper.countByParentId(paremtId);
        if(count==0){
            Category category = new Category();
            category.setId(paremtId);
            category.setIsParent(0);
            rows = categoryMapper.updateById(category);
            if (rows!=1){
                String message = "删除类别失败服务器忙请稍后【错误码2】";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_DELETE,message);
            }
        }
    }

  public List<CategoryListltemVO>listByParentId(Long parentId)
  {
        return categoryMapper.listByParentId(parentId);
  }


}
